package com.ok.community.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ok.api.client.IntegralClient;
import com.ok.api.domain.dto.OkIntegralDetailDTO;
import com.ok.common.config.IntegralNum;
import com.ok.common.domain.po.CardDoc;
import com.ok.common.utils.SnowflakeIdWorker;
import com.ok.common.utils.UserContext;
import com.ok.community.domain.dto.OkCardDTO;
import com.ok.community.domain.po.OkCard;
import com.ok.community.mapper.OkCardMapper;
import com.ok.community.service.IOkCardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.client.RequestOptions;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 帖子 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-29
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OkCardServiceImpl extends ServiceImpl<OkCardMapper, OkCard> implements IOkCardService {

    private final IntegralClient integralClient;
    private final RabbitTemplate rabbitTemplate;

    @Override
    @GlobalTransactional
    public void add(OkCardDTO cardDTO) {
        OkCard okCard = new OkCard();
        BeanUtils.copyProperties(cardDTO,okCard);
        log.info("当前添加帖子的用户id为：{}",UserContext.getUser());
        okCard.setUserId(UserContext.getUser());
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
        long fid = idWorker.nextId();
        okCard.setSnowflakeId(fid);
        //向帖子表添加数据
        this.save(okCard);

        //增加用户积分
        integralClient.update(IntegralNum.INTEGRAL_CARD_ADD);

        //根据雪花id的到帖子对象
        LambdaQueryWrapper<OkCard> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OkCard::getSnowflakeId,fid);
        OkCard card = this.getOne(wrapper);
        log.info("帖子信息:{}",card);
        //构建积分详情信息
        OkIntegralDetailDTO detailDTO = new OkIntegralDetailDTO();
        detailDTO.setUserId(UserContext.getUser());
        detailDTO.setIntegral(IntegralNum.INTEGRAL_CARD_ADD);
        detailDTO.setCardId(card.getId());
        //发送消息添加积分详细
        try {
            rabbitTemplate.convertAndSend("integral_direct","",detailDTO);
        } catch (AmqpException e) {
            log.error("发送消息添加积分详细失败：{}",e.getMessage());
        }

        //封装CardDoc信息
        CardDoc cardDoc = BeanUtil.copyProperties(card, CardDoc.class);
        log.info("cardDoc发送es消息为：{}",cardDoc);
        //发送消息添加es中数据
        try {
            rabbitTemplate.convertAndSend("card_direct","",cardDoc);
        } catch (AmqpException e) {
            log.error("发送消息添加es中帖子失败：{}",e.getMessage());
        }
    }

    @Override
    public void delete(Long ids) {

        this.removeById(ids);

        //发送消息删除es中数据
        try {
            rabbitTemplate.convertAndSend("card_direct","",ids);
        } catch (AmqpException e) {
            log.error("发送消息删除es中帖子失败：{}",e.getMessage());
        }
    }
}
